﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.DistribucionRadicado
{
    class DeleteRadicadoCorrespondenciaCommand : IBaseCommand
    {
         #region [Private Fields]
        
        private string _lista;
        private string _userName;
        private string _idRadicado;
        private Guid _recibido;

        #endregion

        #region [Constructors]


        public DeleteRadicadoCorrespondenciaCommand(string lista, string idRadicado, string userName, Guid recibido)
        {
            _lista = lista;
            _idRadicado = idRadicado;
            _userName = userName;
            _recibido = recibido;
        }

        #endregion
        public void Execute()
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                    using (SPSite ElevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb())
                        {
                            SPList targetList = null;
                            targetList = ElevatedSite.Lists.TryGetList(_lista);
                            ElevatedSite.AllowUnsafeUpdates = true;
                            SPQuery query = new SPQuery();
                            query.Query = string.Concat("<Where>",
                                                            "<Eq>",
                                                                "<FieldRef Name='Document_Radicado' />",
                                                                "<Value Type='Text'>",
                                                                  _idRadicado,
                                                                "</Value>",
                                                            "</Eq>",
                                                        "</Where>");

                            SPListItemCollection splc = targetList.GetItems(query);
                            if (splc.Count > 0)
                            {
                                SPListItem itemList = splc[0];
                                itemList.Delete();
                                //itemList.Update();
                            }
                            ElevatedSite.AllowUnsafeUpdates = false;
                        }
                    }
                });
        }
        public void Undo()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite ElevatedsiteColl = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb())
                    {

                        SPList targetList = null;
                        targetList = ElevatedSite.Lists.GetList(_recibido, false);
                        ElevatedSite.AllowUnsafeUpdates = true;
                        SPQuery query = new SPQuery();
                        query.Query = string.Concat("<Where>",
                                                        "<Eq>",
                                                            "<FieldRef Name='_dlc_DocId' />",
                                                            "<Value Type='Text'>",
                                                              _idRadicado,
                                                            "</Value>",
                                                        "</Eq>",
                                                    "</Where>");
                        SPListItemCollection splc = targetList.GetItems(query);

                        if (splc.Count > 0)
                        {
                            SPListItem itemList = splc[0];
                            itemList[CustomSiteColumns.FIELD_DOCSET_INTERNAL_RECEPTION] = true;
                            itemList.Log(itemList[CustomSiteColumns.FIELD_DOCUMENT_SUBSECCION].ToString(), DateTime.Now, MS.InformationManagement.Support.Enums.TraceableOperation.Distribuido, _userName, "Radicado Distribuido");
                            itemList.Update();
                        }
                        ElevatedSite.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
    }
}
